package com.bwf.ticket;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/19 15:52
 * @Copyright 博为峰
 */
public class Ticket03 implements Runnable {
    private int num = 100;
    final Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            //同步代码块
            synchronized (obj) {
                if (num > 0) {//2
                    try {
                        //t0 1   t1 0
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":num=" + num--);
                } else {
                    break;
                }
            }

        }
    }
}
